package com.levy.contronller.javaWeb.utils;

import com.levy.utils.FileTool.TextFileUtils;
import com.levy.utils.xmlTool.ModifyParam;
import com.levy.utils.xmlTool.ModifyResult;
import com.levy.utils.xmlTool.RelationParam;
import com.levy.utils.xmlTool.RelationResult;
import com.thoughtworks.xstream.XStream;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 郭利伟 on 2017.08.22.
 */
public class XstreamTest {
    private static String xml_head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    /**
     * 解析xml为对象
     * @param xml
     * @return
     */
    public static ModifyParam parseXmlToObject(String xml){
        XStream xStream = new XStream();
        //使用注解时候使用
//        xStream.processAnnotations(ModifyParam.class);
        //将序列化中的类全量名称，用别名替换。
        xStream.alias("modifyParam", ModifyParam.class);
//        xStream.aliasAttribute( ModifyParam.class,"operatorID","operatorID");
//        xStream.aliasAttribute( ModifyParam.class,"relationType","relationType");
        //使用别名替代属性名
        xStream.aliasField("relations", ModifyParam.class,"relations");
        //使用注解时候使用
//        xStream.processAnnotations(RelationParam.class);
        xStream.alias("relation",RelationParam.class);
//        xStream.aliasAttribute(RelationParam.class,"operType","operType");
//        xStream.aliasAttribute(RelationParam.class,"accountID","accountID");
//        xStream.aliasAttribute(RelationParam.class,"privilegeID","privilegeID");
//        xStream.aliasAttribute(RelationParam.class,"startTime","startTime");
//        xStream.aliasAttribute(RelationParam.class,"endTime","endTime");
        ModifyParam modifyParam = (ModifyParam) xStream.fromXML(xml);
        return modifyParam;
    }

    /**
     * 将list对象转换为xml
     * @param list
     * @return
     */
    private static String convertModifyResultListToXml(List<ModifyResult> list){
        StringBuffer buffer = new StringBuffer();
        buffer.append(xml_head);
        XStream xStream = new XStream();
        xStream.setMode(XStream.NO_REFERENCES);
        xStream.alias("results",List.class);
        xStream.alias("result",ModifyResult.class);
        xStream.alias("relation",RelationResult.class);
        String resXml = xStream.toXML(list);
        buffer.append(resXml);
        return buffer.toString();
    }

    /**
     * 解析单个对象为xml
     * @param modifyResult
     * @return
     */
    private static String convertModifyResultToXml(ModifyResult modifyResult){
        StringBuffer buffer = new StringBuffer();
        buffer.append(xml_head);
        XStream xStream = new XStream();
        xStream.alias("result",ModifyResult.class);
        xStream.alias("relation",RelationResult.class);
        String resXml = xStream.toXML(modifyResult);
        buffer.append(resXml);
        return buffer.toString();
    }



    public static void main(String[] args) {
        String xml = TextFileUtils.read("D:\\Idea_workspace\\levy\\src\\main\\java\\com\\levy\\contronller\\javaWeb\\utils\\xmlTool\\xstreamtest.xml");
        System.out.println("读取到的xml如下------");
        System.out.println(xml);


        System.out.println("将xml解析为对象如下------");
        ModifyParam modifyParam = parseXmlToObject(xml);
        System.out.println(modifyParam);


        System.out.println("将单个对象解析为xml如下------");
        List<RelationResult> listRR = new ArrayList<RelationResult>();
        ModifyResult modifyResult = new ModifyResult();
        modifyResult.setResultMsg("ddddd");
        modifyResult.setResultCode("1111111");
        for (int j = 0 ; j<2;j++){
            RelationResult rr = new RelationResult();
            rr.setPrivilegeID(j+"--privilegeID");
            rr.setAccountID(j+"--accountID");
            rr.setRoleID(j+"--roleID");
            listRR.add(rr);
        }
        modifyResult.setRelations(listRR);
        System.out.println(convertModifyResultToXml(modifyResult));

        System.out.println("将list对象解析为xml如下------");
        List<ModifyResult> list = new ArrayList<ModifyResult>();
        List<RelationResult> listR = new ArrayList<RelationResult>();
        for (int i = 0;i<2;i++){
            ModifyResult m = new ModifyResult();
            m.setResultMsg(String.valueOf(i+"--msg"));
            m.setResultCode(String.valueOf(i+"--code"));
                RelationResult rr = new RelationResult();
                rr.setPrivilegeID(i+"--privilegeID");
                rr.setAccountID(i+"--accountID");
                rr.setRoleID(i+"--roleID");
                listR.add(rr);
            m.setRelations(listR);
            list.add(m);
        }
        System.out.println(convertModifyResultListToXml(list));




    }

}
